@echo off
REM 账号密码管理系统 - Windows项目清理脚本
REM 用于清理开发过程中的临时文件和文档,准备发布正式版

chcp 65001 >nul
setlocal enabledelayedexpansion

echo ========================================
echo   账号密码管理系统 - 项目清理脚本
echo ========================================
echo.

REM 切换到项目根目录
cd /d "%~dp0.."
set "PROJECT_ROOT=%CD%"

echo 📂 项目根目录: %PROJECT_ROOT%
echo.

REM 询问用户确认
set /p confirm="⚠️  此操作将清理开发文档和临时文件,是否继续? (y/N): "
if /i not "%confirm%"=="y" (
    echo ❌ 已取消清理操作
    pause
    exit /b 1
)

echo.
echo 🧹 开始清理...
echo.

REM 1. 创建文档目录结构
echo 📁 创建文档目录结构...
if not exist "docs\archive" mkdir "docs\archive"
if not exist "docs\api" mkdir "docs\api"
if not exist "docs\deployment" mkdir "docs\deployment"
if not exist "docs\development" mkdir "docs\development"
if not exist "docs\database" mkdir "docs\database"
if not exist "docs\user-guide" mkdir "docs\user-guide"
if not exist "scripts\deployment" mkdir "scripts\deployment"
echo    ✅ 文档目录创建完成
echo.

REM 2. 归档开发文档
echo 📦 归档开发文档到 docs\archive\...
call :move_file "ADDITIONAL_FEATURES.md" "docs\archive\"
call :move_file "AGENTS.md" "docs\archive\"
call :move_file "BUILD_FIX_SUMMARY.md" "docs\archive\"
call :move_file "CLEANUP_REPORT.md" "docs\archive\"
call :move_file "FEATURES_COMPLETED.md" "docs\archive\"
call :move_file "FINAL_SUMMARY.md" "docs\archive\"
call :move_file "FINAL_SUMMARY_V2.md" "docs\archive\"
call :move_file "I18N_PWA_SUMMARY.md" "docs\archive\"
call :move_file "IMPLEMENTATION_SUMMARY.md" "docs\archive\"
call :move_file "OPTIMIZATION_SUMMARY.md" "docs\archive\"
call :move_file "PROJECT_OPTIMIZATION_REPORT.md" "docs\archive\"
call :move_file "PROJECT_SUMMARY.md" "docs\archive\"
call :move_file "TODO.md" "docs\archive\"
echo.

REM 3. 移动保留的文档
echo 📝 移动保留的文档...
call :move_file "CHANGELOG.md" "docs\"
call :move_file "DEPLOYMENT.md" "docs\deployment\"
call :move_file "PERFORMANCE.md" "docs\development\"
call :move_file "TESTING_GUIDE.md" "docs\development\"
call :move_file "FEATURES.md" "docs\development\"

REM README.md 保留副本
if exist "README.md" (
    copy "README.md" "docs\README.md" >nul 2>&1
    echo    ✅ 已复制: README.md -^> docs/
)
echo.

REM 4. 删除临时文件
echo 🗑️  删除临时文件...
call :delete_file "claude-automation-headless.py"
del /q /f "claude_output_*.log" >nul 2>&1
call :delete_file "tasks.txt"
echo.

REM 5. 删除开发工具目录
echo 🗑️  删除开发工具目录...
if exist "dev-tools" (
    rd /s /q "dev-tools"
    echo    ✅ 已删除: dev-tools\
) else (
    echo    ⚠️  目录不存在: dev-tools\
)
echo.

REM 6. 移动部署脚本
echo 📦 移动部署相关脚本...
call :move_file "deploy.sh" "scripts\deployment\"
call :move_file "deploy.bat" "scripts\deployment\"
call :move_file "docker-compose.yml" "scripts\deployment\"
call :move_file "start.sh" "scripts\"
call :move_file "start.bat" "scripts\"
echo.

REM 7. 创建配置文件示例
echo ⚙️  创建配置文件示例...

if not exist "backend\.env.example" (
    (
        echo APP_DEBUG = false
        echo APP_TRACE = false
        echo.
        echo [DATABASE]
        echo HOSTNAME = 127.0.0.1
        echo DATABASE = accountmanage
        echo USERNAME = root
        echo PASSWORD = your_database_password
        echo HOSTPORT = 3306
        echo CHARSET = utf8mb4
        echo PREFIX = am_
        echo.
        echo [REDIS]
        echo HOST = 127.0.0.1
        echo PORT = 6379
        echo PASSWORD =
        echo SELECT = 0
        echo.
        echo [JWT]
        echo SECRET = your_jwt_secret_key_here_change_in_production
        echo EXPIRE = 7200
        echo.
        echo [ENCRYPT]
        echo KEY = your_encryption_key_here_32_characters_minimum
        echo METHOD = AES-256-CBC
        echo.
        echo [APP]
        echo NAME = 账号密码管理系统
        echo VERSION = 1.0.0
        echo URL = https://yourdomain.com
        echo.
        echo [LOG]
        echo LEVEL = error
        echo TYPE = file
        echo PATH = runtime/log/
    ) > "backend\.env.example"
    echo    ✅ 已创建: backend\.env.example
) else (
    echo    ⚠️  已存在: backend\.env.example
)

if not exist "frontend\.env.example" (
    (
        echo # 应用标题
        echo VITE_APP_TITLE=账号密码管理器
        echo.
        echo # API 基础地址
        echo VITE_API_BASE_URL=http://localhost:8080
        echo.
        echo # API 超时时间（毫秒）
        echo VITE_API_TIMEOUT=10000
        echo.
        echo # 是否启用Mock数据
        echo VITE_USE_MOCK=false
        echo.
        echo # 是否启用PWA
        echo VITE_PWA_ENABLED=true
    ) > "frontend\.env.example"
    echo    ✅ 已创建: frontend\.env.example
) else (
    echo    ⚠️  已存在: frontend\.env.example
)
echo.

REM 8. 生成清理报告
echo 📄 生成清理报告...
(
    echo 账号密码管理系统 - 清理日志
    echo ====================================
    echo.
    echo 清理时间: %date% %time%
    echo.
    echo 清理内容:
    echo ---------
    echo.
    echo 1. 已归档的开发文档:
    echo    - ADDITIONAL_FEATURES.md
    echo    - AGENTS.md
    echo    - BUILD_FIX_SUMMARY.md
    echo    - CLEANUP_REPORT.md
    echo    - FEATURES_COMPLETED.md
    echo    - FINAL_SUMMARY.md
    echo    - FINAL_SUMMARY_V2.md
    echo    - I18N_PWA_SUMMARY.md
    echo    - IMPLEMENTATION_SUMMARY.md
    echo    - OPTIMIZATION_SUMMARY.md
    echo    - PROJECT_OPTIMIZATION_REPORT.md
    echo    - PROJECT_SUMMARY.md
    echo    - TODO.md
    echo.
    echo 2. 已移动的保留文档:
    echo    - CHANGELOG.md -^> docs\
    echo    - DEPLOYMENT.md -^> docs\deployment\
    echo    - PERFORMANCE.md -^> docs\development\
    echo    - TESTING_GUIDE.md -^> docs\development\
    echo    - README.md ^(复制^) -^> docs\
    echo.
    echo 3. 已删除的临时文件:
    echo    - claude-automation-headless.py
    echo    - claude_output_*.log
    echo    - tasks.txt
    echo    - dev-tools\ ^(目录^)
    echo.
    echo 4. 已移动的脚本:
    echo    - deploy.sh -^> scripts\deployment\
    echo    - deploy.bat -^> scripts\deployment\
    echo    - docker-compose.yml -^> scripts\deployment\
    echo    - start.sh -^> scripts\
    echo    - start.bat -^> scripts\
    echo.
    echo 5. 已创建的配置文件:
    echo    - backend\.env.example
    echo    - frontend\.env.example
    echo.
    echo 下一步:
    echo -------
    echo 1. 查看 RELEASE_CHECKLIST.md 了解发布前的完整检查清单
    echo 2. 修复严重的安全问题（特别是AES加密）
    echo 3. 创建生产环境配置文件（复制.env.example并填写实际值）
    echo 4. 完善文档（API文档、部署文档等）
    echo 5. 进行全面测试
) > CLEANUP_LOG.txt

echo    ✅ 清理报告已保存到: CLEANUP_LOG.txt
echo.

echo ========================================
echo ✅ 清理完成！
echo ========================================
echo.
echo 📋 清理摘要:
echo    - 已归档 13 个开发文档到 docs\archive\
echo    - 已移动 5 个保留文档到 docs\
echo    - 已删除临时文件和开发工具
echo    - 已创建配置文件示例
echo.
echo 📝 下一步建议:
echo    1. 查看 RELEASE_CHECKLIST.md 了解发布清单
echo    2. 查看 CLEANUP_LOG.txt 了解详细清理记录
echo    3. 复制配置文件: copy backend\.env.example backend\.env
echo    4. 修改配置文件中的敏感信息（密钥、密码等）
echo    5. 修复严重安全问题（参考 RELEASE_CHECKLIST.md）
echo.
echo 💡 提示: 所有开发文档已归档到 docs\archive\,可随时查看
echo.

pause
exit /b 0

:move_file
if exist "%~1" (
    move "%~1" "%~2" >nul 2>&1
    echo    ✅ 已移动: %~1 -^> %~2
) else (
    echo    ⚠️  文件不存在: %~1
)
exit /b 0

:delete_file
if exist "%~1" (
    del /q /f "%~1" >nul 2>&1
    echo    ✅ 已删除: %~1
) else (
    echo    ⚠️  文件不存在: %~1
)
exit /b 0
